package com.znufe.exam.controller.student;

import com.znufe.exam.base.BaseApiController;
import com.znufe.exam.base.RestResponse;
import com.znufe.exam.base.SystemCode;
import com.znufe.exam.domain.ExamPaper;
import com.znufe.exam.domain.SubCourse;
import com.znufe.exam.service.ExamPaperService;
import com.znufe.exam.service.SubCourseExamPaperService;
import com.znufe.exam.service.SubCourseService;
import com.znufe.exam.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.znufe.exam.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.znufe.exam.viewmodel.admin.exam.ExamQuestionListResponseVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**   
 * 
 * @Description:  课程表接口层
 * @Author:       gong   
 * @CreateDate:   Course
 * @Version:      V1.0
 *    
 */
@Api(description = "子课程信息表",value="子课程信息表" )
@RestController
@RequestMapping("/api/user/subCourse")
public class SubCourseController extends BaseApiController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	public SubCourseService subCourseService;
	@Autowired
	public SubCourseExamPaperService subCourseExamPaperService;
	@Autowired
    public ExamPaperService examPaperService;

    @RequestMapping(value = "/getExamPaper/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "获取视频绑定的试卷及对应试题",notes = "参数为视频ID")
    public RestResponse<List<ExamQuestionListResponseVM>> getExamPaper(@PathVariable("id")Integer id){
	    List<Integer> examPaperId = subCourseExamPaperService.getAllExamPaperId(id);
	    if (examPaperId.size() == 0){
	        return RestResponse.ok(Collections.emptyList());
        }else {
            ArrayList<ExamQuestionListResponseVM> list = new ArrayList<>();
	        for (int i=0;i<examPaperId.size();i++){
                ExamPaperEditRequestVM examPaperEditRequestVM = examPaperService.examPaperToVM(examPaperId.get(i));
                ExamQuestionListResponseVM vm = modelMapper
                        .map(examPaperEditRequestVM, ExamQuestionListResponseVM.class);
                ExamPaperTitleItemVM questionListVM = examPaperEditRequestVM.getTitleItems().get(0);
                vm.setExamName(questionListVM.getName());
                vm.setQuestionItems(questionListVM.getQuestionItems());
                list.add(vm);
            }
            return RestResponse.ok(list);
        }
    }
}